package notice.service.impl;

import lombok.extern.slf4j.Slf4j;
import notice.dto.OrderNoticeDto;
import notice.service.Observer;
import notice.util.IdempotentUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 扣款通知：垫资处理
 * @author: zk
 * @since: 2022-08-17 10:19
 */
@Slf4j
@Service
public class AdvanceHandler implements Observer {
    //@Resource
    //private WXCarOwnerPlatformService wxCarOwnerPlatformService;
    //@Resource
    //private CarWxOverdueDao carWxOverdueDao;
    //@Resource
    //private TruckCardBlackService cardBlackService;
    //@Resource
    //private CarDeductionRecordServiceImpl carDeductionRecordServiceImpl;
    @Resource
    private IdempotentUtil idempotentUtil;

    @Override
    public void update(OrderNoticeDto notice) {
        log.info("扣款通知-垫资处理 notice={}", notice);
        // 幂等检查
        if (idempotentUtil.idempotentCheck(this.getClass(), notice.getOrderNo())) {
            log.warn("扣款通知-垫资处理 该记录已经处理过 notice={}", notice);
            return;
        }

        // 客车垫资处理
        //if(OrderNoticeDto.ORDER_TYPE_CAR.equals(notice.getOrderType())) {
        //    //微信代扣成功-发生垫资
        //    if(CarDeductionRecord.CHANNEL_WX.equals(notice.getCurrPayChannel())
        //        && CarDeductionRecord.PAY_STATUS_SUCCES.equals(notice.getPayStatus())) {
        //            handleBeforehandWithhold(notice, PayBizUtils.getBizCode(notice.getOrderNo()));
        //    }
        //}

        //  幂等表-有效期一周
        idempotentUtil.addIdempotentRecord(this.getClass(), notice.getOrderNo(), 7 * 24 * 60 * 60L);
    }

//    private void handleBeforehandWithhold(OrderNoticeDto notice,String bizCode) {
//        log.info("微信代扣成功,属于微信垫资处理-notice={}",notice);
//        Map<String, String> map = null;
//        try {
//            map = wxCarOwnerPlatformService.queryState(notice.getPlateNumber(), bizCode);
//            String userState = map.get("user_state");
//            //垫资欠款
//            if ("OVERDUE".equalsIgnoreCase(userState)) {
//                if (EtcIssueTypeEnum.ZS_CAR.getCode().equals(notice.getEtcTypeId())) {
//                    //如果微信垫资，先调用拉黑接口，在保存代扣结果，防止拉黑失败
//                    cardBlackService.addBlack(notice.getPlateNumber(), notice.getEtcNo(), TruckCardBlackLog.USER_ID_SYSTEM, TruckCardBlackLog.USER_NAME_SYSTEM, "代扣成功,属于微信垫资", TruckCardBlack.SCENE_TRANS);
//                }
//                CarWxOverdue carWxOverdue = new CarWxOverdue();
//                carWxOverdue.setBizCode(bizCode);
//                carWxOverdue.setChannel(notice.getChannel());
//                carWxOverdue.setColor(notice.getColor());
//                carWxOverdue.setPlateNumber(notice.getPlateNumber());
//                carWxOverdue.setEtcNo(notice.getEtcNo());
//                carWxOverdue.setOrderNo(String.valueOf(SnowflakeIdWorker.nextID()));
//                carWxOverdue.setCreateTime(new Date());
//                carWxOverdue.setUpdateTime(new Date());
//                carWxOverdue.setEtcTypeId(notice.getEtcTypeId());
//                carWxOverdueDao.insert(carWxOverdue);
//                log.info("微信代扣成功,属于微信垫资-小车微信垫资车牌信息插入-carWxOverdue={}",carWxOverdue);
//                if (EtcIssueTypeEnum.ZS_CAR.getCode().equals(notice.getEtcTypeId())) {
//                    if (carDeductionRecordServiceImpl.timeInterval(11, 20)) {
//                        log.info("微信代扣成功,属于微信垫资-发送通知短信");
//                        //发送短信
//                        CarDeductionRecord carDeductionRecord = new CarDeductionRecord();
//                        BeanUtils.copyProperties(notice,carDeductionRecord);
//                        carDeductionRecordServiceImpl.sendMsg(carDeductionRecord);
//                    }
//                }
//            }
//        } catch (Exception e) {
//            log.warn("微信代扣成功,属于微信垫资处理异常-notice={},map={}",notice,map,e);
//        }
//}
}
